/**
 * Created by ZY on 2017/12/25
 */
let _ = require('lodash'),
  util = require('../helper/util'),
  co = require('co'),
  md5 = require('md5'),
  client = require('../helper/oss'),
  errCode = require('../../conf/errorCode'),
  fs = require('fs'),
  logger = require('../helper/logger');

var tagLogger = new logger.TagLogger('COMMON_CONTROLLER');
exports.upload = function(req, res, next) {
  let {tkey} = req.body,
    buffer = [],
    size = 0,
    sign_file1 = '';
  co(function*() {
    //client.useBucket('carplus');
    var result = yield client.putStream(
      md5(req.files[0].buffer),
      util.bufferToStream(req.files[0].buffer),
      {
          headers:{
            'Content-Type':'image/jpeg'
          }
      }
    );
    result.url = result.url.replace('http:','https:');
    return util.getHandle(
      req,
      res,
      errCode.UPLOAD_IMG_TO_DFS,
    )(null, result);
  }).catch(function(err) {
    tagLogger.info(err);
    if (err) {
      return util.getHandle(req, res, errCode.UPLOAD_IMG_TO_DFS)({
        errNo: errCode.UPLOAD_IMG_TO_DFS,
      });
    }
  });
};
